package com.gryffonx.Interfaces.generators;


import com.gryffonx.NameGenerators.*;
import com.gryffonx.utils.TimelineTableCellRenderer;
import com.gryffonx.utils.SortJTableUtil;
import com.gryffonx.DiceRoller.StaticDiceRoller;
import com.gryffonx.artgenerator.ArtworkIdeaGenerator;
import com.main.UIOptions.NameGeneratorOptionTree;
import com.main.UIOptions.UIActionListeners;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumnModel;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import java.awt.event.*;
import java.awt.*;

/**
 * @author Erik Stell
 * @version 1.0
 */
public class NameGeneratorsBinding implements ActionListener {
    private JPanel pnlMainPanel;
    private JScrollPane scrlTableResults;
    private JTable tblSavedResults;
    private JScrollPane scrlTextResults;
    private JScrollPane scrlOptionTree;
    private JTree treeOptions;
    private JSpinner spnrHowMany;
    private JButton btnGenerate;
    private JButton btnClearTextArea;
    private JButton btnSaveTable;
    private JButton btnClearTable;
    private JLabel lblHowMany;
    private JButton btnAddToTable;
    private DefaultTableModel SavedTableModel;
    private DefaultTableModel GeneratedTableModel;
    private JTable tblGeneratedResults;
    private JCheckBox addTitleToRaceCheckBox;
    private JComboBox comboBox1;
    private JTextPane textPane1;
    private JTabbedPane tabbedPane1;
    private JPanel pnlPlot;
    private JPanel pnlNames;
    private JPanel optionPane;
    private JScrollPane pnTbleScroller;
    private JTable tblTimelineTable;
    private JLabel lblStartYear;
    private JTextField txtStartYear;
    private JTextField txtEndYear;
    private JLabel lblEndYear;
    private JButton btnTimelineGenerate;
    private JButton btnClearTimeline;
    private JButton btnSaveTimeline;
    private JButton btnLoadTimeline;
    private JComboBox comboBox2;
    private JButton generateButton;
    private JTextArea textArea1;
    private JButton generateButton1;
    private JTextArea textArea2;
    private JButton saveButton;
    private JButton clearButton;
    private JButton loadButton;

    public JButton getSaveButton() {
        return saveButton;
    }

    public void setSaveButton(JButton saveButton) {
        this.saveButton = saveButton;
    }

    public JButton getClearButton() {
        return clearButton;
    }

    public void setClearButton(JButton clearButton) {
        this.clearButton = clearButton;
    }

    public JButton getLoadButton() {
        return loadButton;
    }

    public void setLoadButton(JButton loadButton) {
        this.loadButton = loadButton;
    }

    public JTextPane getTextPane1() {
        return textPane1;
    }

    public JButton getGenerateButton1() {
        return generateButton1;
    }

    public void setGenerateButton1(JButton generateButton1) {
        this.generateButton1 = generateButton1;
    }

    public JButton getGenerateButton() {
        return generateButton;
    }

    public void setGenerateButton(JButton generateButton) {
        this.generateButton = generateButton;
    }

    public void setTextPane1(JTextPane textPane1) {
        this.textPane1 = textPane1;
    }

    public DefaultTableModel getSavedTableModel() {
        return SavedTableModel;
    }

    public void setSavedTableModel(DefaultTableModel savedTableModel) {
        this.SavedTableModel = savedTableModel;
    }

    public DefaultTableModel getGeneratedTableModel() {
        return GeneratedTableModel;
    }

    public void setGeneratedTableModel(DefaultTableModel generatedTableModel) {
        this.GeneratedTableModel = generatedTableModel;
    }

    public JTable getTblGeneratedResults() {
        return tblGeneratedResults;
    }

    public void setTblGeneratedResults(JTable tblGeneratedResults) {
        this.tblGeneratedResults = tblGeneratedResults;
    }


    public JTextArea getTaResults() {
        return taResults;
    }

    public void setTaResults(JTextArea taResults) {
        this.taResults = taResults;
    }

    private JTextArea taResults;


    public NameGeneratorsBinding() {

        //set look and feel
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }        
        JFileChooser fc = new JFileChooser();
        DefaultTreeModel treeModel = new DefaultTreeModel(NameGeneratorOptionTree.buildNavigationTree());

        spnrHowMany.setValue(1);
        treeOptions.setModel(treeModel);
        /**
        treeOptions.setCellRenderer(new DefaultTreeCellRenderer(){
            public Component getTreeCellRendererComponent(JTree pTree, Object pValue, boolean pIsSelected, boolean pIsExpanded, boolean pIsLeaf, int pRow, boolean pHasFocus){
                DefaultMutableTreeNode node = (DefaultMutableTreeNode)pValue;
                super.getTreeCellRendererComponent(pTree, pValue, pIsSelected, pIsExpanded, pIsLeaf, pRow, pHasFocus);
                setOpaque(false);
                setBackgroundNonSelectionColor(new Color(102,102,102));
                setBackground(new Color(102,102,102));
                    setForeground(Color.white);
                return (this);
            }
        });
         **/


        //set the default model for the table
        Object[] colName = new Object[1];
        colName[0] = "Saved Results";

        Object[] intRest = new Object[1];
        intRest[0] = "Generated Results";
        TimelineTableCellRenderer cellRenderer = new TimelineTableCellRenderer();

        GeneratedTableModel = new DefaultTableModel(intRest, 0);
        tblGeneratedResults.setRowSelectionAllowed(true);
        tblGeneratedResults.setColumnSelectionAllowed(true);
        tblGeneratedResults.setModel(GeneratedTableModel);

        SavedTableModel = new DefaultTableModel(colName, 0);
        tblSavedResults.setRowSelectionAllowed(true);
        tblSavedResults.setColumnSelectionAllowed(true);
        tblSavedResults.setModel(SavedTableModel);

        TableColumnModel colModel1 = tblSavedResults.getColumnModel();
        colModel1.getColumn(0).setCellRenderer(cellRenderer);

        TableColumnModel colModel2 = tblGeneratedResults.getColumnModel();
        colModel2.getColumn(0).setCellRenderer(cellRenderer);



        //set the default model for the table
        Object[] colName2 = new Object[2];
        colName2[0] = "Year";
        colName2[1] = "Event";

        TimelineTableCellRenderer cellRenderer2 = new TimelineTableCellRenderer();
        SavedTableModel = new DefaultTableModel(colName2, 0);

        tblTimelineTable.setRowSelectionAllowed(true);
        tblTimelineTable.setColumnSelectionAllowed(true);

        tblTimelineTable.setModel(SavedTableModel);

        TableColumnModel colModel = tblTimelineTable.getColumnModel();
        colModel.getColumn(0).setPreferredWidth(50);
        colModel.getColumn(0).setCellRenderer(cellRenderer2);
        colModel.getColumn(1).setPreferredWidth(350);
        colModel.getColumn(1).setCellRenderer(cellRenderer2);

        //set the action listeners

        MouseListener ml = new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                int selRow = treeOptions.getRowForLocation(e.getX(), e.getY());
                TreePath selPath = treeOptions.getPathForLocation(e.getX(), e.getY());
                if (selRow != -1) {
                    if (e.getClickCount() == 2) {
                        try {
                            performGeneration(selPath.getPath());
                        } catch (Exception e1) {
                            e1.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                        }
                    }
                }
            }
        };
        treeOptions.addMouseListener(ml);


        btnGenerate.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                try {
                    performGeneration(treeOptions.getLeadSelectionPath().getPath());
                } catch (Exception e) {
                    e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                }
            }
        });

        generateButton1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                ArtworkIdeaGenerator generator = new ArtworkIdeaGenerator();
                try {
                    textArea2.setText(textArea2.getText() + generator.generateIdea() + "\r\n");
                } catch (Exception e) {
                    e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                }

            }
        });
        clearButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                textArea2.setText("");
            }
        });

        btnClearTextArea.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                GeneratedTableModel.setRowCount(0);
                tblGeneratedResults.setModel(GeneratedTableModel);
            }
        });

        btnClearTable.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                SavedTableModel.setRowCount(0);
                tblSavedResults.setModel(SavedTableModel);

            }
        });

        btnAddToTable.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                if (tblGeneratedResults.getRowCount() >= 1) {
                    try {
                        int index = tblGeneratedResults.getSelectedRow();
                        String selected = tblGeneratedResults.getValueAt(index, 0).toString();
                        if (selected != null && !selected.equals("")) {
                            Object[] myArry = {selected};

                            SavedTableModel.addRow(myArry);
                            tblSavedResults.setModel(SavedTableModel);
                        }
                    } catch (Exception e) {
                        taResults.setText("An error occured during this operation");
                    }
                }
            }
        });

        btnTimelineGenerate.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                generateTimeLine();
            }
        });


        btnClearTimeline.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                SavedTableModel.setRowCount(0);
                tblTimelineTable.setModel(SavedTableModel);
            }
        });

        btnLoadTimeline.addActionListener(UIActionListeners.openAction(this));

        btnSaveTimeline.addActionListener(UIActionListeners.saveAction(this));        

        btnSaveTable.addActionListener(UIActionListeners.saveAction(this));

    }

    private void performGeneration(Object[] strPaths) throws Exception {
        if (treeOptions.getSelectionPath() != null) {
            TreePath paths = treeOptions.getLeadSelectionPath();
            //Object[] strPaths = paths.getPath();
            String gender = "";
            String race;
            String option = "";

            String rootPath = strPaths[1].toString();
            if (!rootPath.equals("Locations") && !rootPath.equals("Miscellaneous")) {
                option = strPaths[3].toString();
                gender = comboBox1.getSelectedItem().toString();
                race = strPaths[2].toString();
            } else {
                race = strPaths[2].toString();
            }
            StringBuffer testBuffer = new StringBuffer();
            int howMany = Integer.parseInt(spnrHowMany.getValue().toString());

            //taResults.setText(testBuffer.toString());

            //check what kind of generator to run
            if (rootPath.equals("Races")) {
                if (race.equals("Humans")) {

                    if (howMany <= 0) {
                        howMany = 1;
                    }
                    for (int i = 0; i < howMany; i++) {
                        if (option.equals("Given")){
                            Object[] myArry = {HumanNameGenerator.generateName(gender, 1, addTitleToRaceCheckBox.isSelected(), false)};
                            GeneratedTableModel.addRow(myArry);
                        }else if (option.equals("Surname")){
                            Object[] myArry = {HumanNameGenerator.generateSurname()};
                            GeneratedTableModel.addRow(myArry);
                        }else if (option.equals("Both")){
                            Object[] myArry = {HumanNameGenerator.generateName(gender, 1, addTitleToRaceCheckBox.isSelected(), true)};
                            GeneratedTableModel.addRow(myArry);                            
                        }


                    }
                    tblGeneratedResults.setModel(GeneratedTableModel);
                } else if (race.equals("Elves")) {

                    if (howMany == 0) {
                        howMany = 1;
                    }
                    for (int i = 0; i < howMany; i++) {

                        if (option.equals("Given")){
                            Object[] myArry = {ElfNameGenerator.generateName(gender, 1, addTitleToRaceCheckBox.isSelected(), false)};
                            GeneratedTableModel.addRow(myArry);
                        }else if (option.equals("Surname")){
                            Object[] myArry = {ElfNameGenerator.generateSurname()};
                            GeneratedTableModel.addRow(myArry);
                        }else if (option.equals("Both")){
                            Object[] myArry = {ElfNameGenerator.generateName(gender, 1, addTitleToRaceCheckBox.isSelected(), true)};
                            GeneratedTableModel.addRow(myArry);
                        }
                    }
                    tblGeneratedResults.setModel(GeneratedTableModel);

                } else if (race.equals("Draconic")) {

                    if (howMany == 0) {
                        howMany = 1;
                    }
                    for (int i = 0; i < howMany; i++) {
                        Object[] myArry = {DraconicNameGenerator.generateName(gender, 1)};
                        GeneratedTableModel.addRow(myArry);
                    }
                    tblGeneratedResults.setModel(GeneratedTableModel);

                } else if (race.equals("Drow")) {

                    if (howMany == 0) {
                        howMany = 1;
                    }
                    for (int i = 0; i < howMany; i++) {


                        if (option.equals("Given")){
                            Object[] myArry = {DrowNameGenerator.generateName(gender, 1, addTitleToRaceCheckBox.isSelected(), false)};
                            GeneratedTableModel.addRow(myArry);
                        }else if (option.equals("Surname")){
                            Object[] myArry = {DrowNameGenerator.generateSurname()};
                            GeneratedTableModel.addRow(myArry);
                        }else if (option.equals("Both")){
                            Object[] myArry = {DrowNameGenerator.generateName(gender, 1, addTitleToRaceCheckBox.isSelected(), true)};
                            GeneratedTableModel.addRow(myArry);
                        }
                    }
                    tblGeneratedResults.setModel(GeneratedTableModel);
                } else if (race.equals("Dwarves")) {

                    if (howMany == 0) {
                        howMany = 1;
                    }
                    for (int i = 0; i < howMany; i++) {

                        if (option.equals("Given")){
                            Object[] myArry = {DwarfNameGenerator.generateName(gender, 1, addTitleToRaceCheckBox.isSelected(), false)};
                            GeneratedTableModel.addRow(myArry);
                        }else if (option.equals("Surname")){
                            Object[] myArry = {DwarfNameGenerator.generateSurname()};
                            GeneratedTableModel.addRow(myArry);
                        }else if (option.equals("Both")){
                            Object[] myArry = {DwarfNameGenerator.generateName(gender, 1, addTitleToRaceCheckBox.isSelected(), true)};
                            GeneratedTableModel.addRow(myArry);
                        }

                    }
                    tblGeneratedResults.setModel(GeneratedTableModel);

                } else if (race.equals("Empyric")) {

                    if (howMany == 0) {
                        howMany = 1;
                    }
                    for (int i = 0; i < howMany; i++) {

                        if (option.equals("Given")){
                            Object[] myArry = {EmpyrealNameGenerator.generateName(gender, 1, addTitleToRaceCheckBox.isSelected(), false)};
                            GeneratedTableModel.addRow(myArry);
                        }else if (option.equals("Surname")){
                            Object[] myArry = {EmpyrealNameGenerator.generateSurname()};
                            GeneratedTableModel.addRow(myArry);
                        }else if (option.equals("Both")){
                            Object[] myArry = {EmpyrealNameGenerator.generateName(gender, 1, addTitleToRaceCheckBox.isSelected(), true)};
                            GeneratedTableModel.addRow(myArry);
                        }
                    }
                    tblGeneratedResults.setModel(GeneratedTableModel);

                } else if (race.equals("Fae")) {

                    if (howMany == 0) {
                        howMany = 1;
                    }
                    for (int i = 0; i < howMany; i++) {
                        Object[] myArry = {FaeryNameGenerator.generateName(gender, 1, addTitleToRaceCheckBox.isSelected())};
                        GeneratedTableModel.addRow(myArry);
                    }
                    tblGeneratedResults.setModel(GeneratedTableModel);
                } else if (race.equals("Giants")) {
                    //testBuffer.append(ElfNameGenerator.generateName(strPaths[3].toString(), howMany));
                    testBuffer.append("Not Implemented Yet\n\r");
                } else if (race.equals("Gnomes")) {
                    if (howMany == 0) {
                        howMany = 1;
                    }
                    for (int i = 0; i < howMany; i++) {

                        if (option.equals("Given")){
                            Object[] myArry = {GnomeNameGenerator.generateName(gender, 1, addTitleToRaceCheckBox.isSelected(), false)};
                            GeneratedTableModel.addRow(myArry);
                        }else if (option.equals("Surname")){
                            Object[] myArry = {GnomeNameGenerator.generateSurname()};
                            GeneratedTableModel.addRow(myArry);
                        }else if (option.equals("Both")){
                            Object[] myArry = {GnomeNameGenerator.generateName(gender, 1, addTitleToRaceCheckBox.isSelected(), true)};
                            GeneratedTableModel.addRow(myArry);
                        }
                    }
                    tblGeneratedResults.setModel(GeneratedTableModel);
                } else if (race.equals("Goblins")) {
                    if (howMany == 0) {
                        howMany = 1;
                    }
                    for (int i = 0; i < howMany; i++) {
                        Object[] myArry = {GoblinNameGenerator.generateName(gender, 1, addTitleToRaceCheckBox.isSelected())};
                        GeneratedTableModel.addRow(myArry);
                    }
                    tblGeneratedResults.setModel(GeneratedTableModel);
                } else if (race.equals("Halflings")) {
                    if (howMany == 0) {
                        howMany = 1;
                    }
                    for (int i = 0; i < howMany; i++) {

                        if (option.equals("Given")){
                            Object[] myArry = {HalflingNameGenerator.generateName(gender, 1, addTitleToRaceCheckBox.isSelected(), false)};
                            GeneratedTableModel.addRow(myArry);
                        }else if (option.equals("Surname")){
                            Object[] myArry = {HalflingNameGenerator.generateSurname()};
                            GeneratedTableModel.addRow(myArry);
                        }else if (option.equals("Both")){
                            Object[] myArry = {HalflingNameGenerator.generateName(gender, 1, addTitleToRaceCheckBox.isSelected(), true)};
                            GeneratedTableModel.addRow(myArry);
                        }
                    }
                    tblGeneratedResults.setModel(GeneratedTableModel);
                } else if (race.equals("Infernal")) {
                    //testBuffer.append(ElfNameGenerator.generateName(strPaths[3].toString(), howMany));
                    testBuffer.append("Not Implemented Yet\n\r");
                } else if (race.equals("Ogres")) {
                    if (howMany == 0) {
                        howMany = 1;
                    }
                    for (int i = 0; i < howMany; i++) {
                        Object[] myArry = {OgreNameGenerator.generateName(gender, 1, addTitleToRaceCheckBox.isSelected())};
                        GeneratedTableModel.addRow(myArry);
                    }
                    tblGeneratedResults.setModel(GeneratedTableModel);
                } else if (race.equals("Orcs")) {
                    if (howMany == 0) {
                        howMany = 1;
                    }
                    for (int i = 0; i < howMany; i++) {
                        Object[] myArry = {OrcNameGenerator.generateName(gender, 1, addTitleToRaceCheckBox.isSelected())};
                        GeneratedTableModel.addRow(myArry);
                    }
                    tblGeneratedResults.setModel(GeneratedTableModel);
                }
            } else if (rootPath.equals("Locations")) {
                if (race.equals("Nations")) {
                    if (howMany == 0) {
                        howMany = 1;
                    }
                    for (int i = 0; i < howMany; i++) {
                        Object[] myArry = {NationNameGenerator.generateName(1)};
                        GeneratedTableModel.addRow(myArry);
                    }
                    tblGeneratedResults.setModel(GeneratedTableModel);
                } else if (race.equals("Taverns")) {
                    if (howMany == 0) {
                        howMany = 1;
                    }
                    for (int i = 0; i < howMany; i++) {
                        Object[] myArry = {TavernNameGenerator.generateName(1)};
                        GeneratedTableModel.addRow(myArry);
                    }
                    tblGeneratedResults.setModel(GeneratedTableModel);
                } else if (race.equals("Cities")) {
                    if (howMany == 0) {
                        howMany = 1;
                    }
                    for (int i = 0; i < howMany; i++) {
                        Object[] myArry = {CityNameGenerator.generateName(1)};
                        GeneratedTableModel.addRow(myArry);
                    }
                    tblGeneratedResults.setModel(GeneratedTableModel);
                } else if (race.equals("Villages")) {
                    if (howMany == 0) {
                        howMany = 1;
                    }
                    for (int i = 0; i < howMany; i++) {
                        Object[] myArry = {VillageNameGenerator.generateName(1)};
                        GeneratedTableModel.addRow(myArry);
                    }
                    tblGeneratedResults.setModel(GeneratedTableModel);
                } else if (race.equals("Landmarks")) {
                    if (howMany == 0) {
                        howMany = 1;
                    }
                    for (int i = 0; i < howMany; i++) {
                        Object[] myArry = {LandmarkNameGenerator.generateName(1)};
                        GeneratedTableModel.addRow(myArry);
                    }
                    tblGeneratedResults.setModel(GeneratedTableModel);
                } else if (race.equals("Forests")) {
                    //TODO: change to use a forest generator instead of a landmark generator
                    if (howMany == 0) {
                        howMany = 1;
                    }
                    for (int i = 0; i < howMany; i++) {
                        Object[] myArry = {LandmarkNameGenerator.generateName(1)};
                        GeneratedTableModel.addRow(myArry);
                    }
                    tblGeneratedResults.setModel(GeneratedTableModel);
                } 
            } else if (rootPath.equals("Miscellaneous")) {
                if (race.equals("Organizations")) {
                    if (howMany == 0) {
                        howMany = 1;
                    }
                    for (int i = 0; i < howMany; i++) {
                        Object[] myArry = {OrganizationNameGenerator.generateName(1, this)};
                        GeneratedTableModel.addRow(myArry);
                    }
                    tblGeneratedResults.setModel(GeneratedTableModel);
                } else if (race.equals("Artifacts")) {
                    if (howMany == 0) {
                        howMany = 1;
                    }
                    for (int i = 0; i < howMany; i++) {
                        Object[] myArry = {ArtifactNameGenerator.generateName(1)};
                        GeneratedTableModel.addRow(myArry);
                    }
                    tblGeneratedResults.setModel(GeneratedTableModel);
                } else if (race.equals("Titles")) {
                    if (howMany == 0) {
                        howMany = 1;
                    }
                    for (int i = 0; i < howMany; i++) {
                        Object[] myArry = {TitleGenerator.generateName(1)};
                        GeneratedTableModel.addRow(myArry);
                    }
                    tblGeneratedResults.setModel(GeneratedTableModel);
                }
            }
        }
    }

    public void actionPerformed(ActionEvent e) {


    }


    public JPanel getPnlMainPanel() {
        return pnlMainPanel;
    }

    public void setPnlMainPanel(JPanel pnlMainPanel) {
        this.pnlMainPanel = pnlMainPanel;
    }

    public JScrollPane getScrlTableResults() {
        return scrlTableResults;
    }

    public void setScrlTableResults(JScrollPane scrlTableResults) {
        this.scrlTableResults = scrlTableResults;
    }

    public JTable getTblSavedResults() {
        return tblSavedResults;
    }

    public void setTblSavedResults(JTable tblSavedResults) {
        this.tblSavedResults = tblSavedResults;
    }

    public JScrollPane getScrlTextResults() {
        return scrlTextResults;
    }

    public void setScrlTextResults(JScrollPane scrlTextResults) {
        this.scrlTextResults = scrlTextResults;
    }

    public JScrollPane getScrlOptionTree() {
        return scrlOptionTree;
    }

    public void setScrlOptionTree(JScrollPane scrlOptionTree) {
        this.scrlOptionTree = scrlOptionTree;
    }

    public JTree getTreeOptions() {
        return treeOptions;
    }

    public void setTreeOptions(JTree treeOptions) {
        this.treeOptions = treeOptions;
    }

    public JSpinner getSpnrHowMany() {
        return spnrHowMany;
    }

    public void setSpnrHowMany(JSpinner spnrHowMany) {
        this.spnrHowMany = spnrHowMany;
    }

    public JButton getBtnGenerate() {
        return btnGenerate;
    }

    public void setBtnGenerate(JButton btnGenerate) {
        this.btnGenerate = btnGenerate;
    }

    public JButton getBtnClearTextArea() {
        return btnClearTextArea;
    }

    public void setBtnClearTextArea(JButton btnClearTextArea) {
        this.btnClearTextArea = btnClearTextArea;
    }

    public JButton getBtnSaveTable() {
        return btnSaveTable;
    }

    public void setBtnSaveTable(JButton btnSaveTable) {
        this.btnSaveTable = btnSaveTable;
    }

    public JButton getBtnClearTable() {
        return btnClearTable;
    }

    public void setBtnClearTable(JButton btnClearTable) {
        this.btnClearTable = btnClearTable;
    }

    public JLabel getLblHowMany() {
        return lblHowMany;
    }

    public void setLblHowMany(JLabel lblHowMany) {
        this.lblHowMany = lblHowMany;
    }

    public JButton getBtnAddToTable() {
        return btnAddToTable;
    }

    public void setBtnAddToTable(JButton btnAddToTable) {
        this.btnAddToTable = btnAddToTable;
    }

    private void generateTimeLine() {
        if (txtStartYear.getText().length() > 0 && txtEndYear.getText().length() > 0) {
            System.out.println("I clicked the generate button with these values " + txtStartYear.getText() + " & " + txtEndYear.getText());

            //step 1 - take the start and end dates and make an integer out of them
            int startYear;
            int endYear;
            try {
                startYear = Integer.parseInt(txtStartYear.getText());
                endYear = Integer.parseInt(txtEndYear.getText());
            } catch (NumberFormatException e) {
                JOptionPane.showMessageDialog(btnTimelineGenerate, "Please enter only numeric values!");
                return;
            }
            int yearDiff;

            //step 2 - subtract the startyear from the end year and assign it to year diff
            yearDiff = (endYear - startYear);
            //if year diff returns zero or a negative number, display an error and halt.
            if (yearDiff <= 0) {
                JOptionPane.showMessageDialog(btnTimelineGenerate, "The difference between the two years must be greater than zero!");
                return;
            } else if (yearDiff > 1500) {
                int retVal = JOptionPane.showConfirmDialog(btnTimelineGenerate, "You are attempting to generate a list for over 1500 years.  This could take a VERY long time.  Are you sure you want to do this?");
                if (retVal == 1 || retVal == 2) {
                    return;
                }


            }

            //clear out the original results
            SavedTableModel.setRowCount(0);
            tblTimelineTable.setModel(SavedTableModel);

            //Step 3 - use the differen
            // ce between the years as a basis for the random number of events that occured
            //within those years
            int randomOccurences = StaticDiceRoller.rolldSomething(yearDiff, 1) + 1;
            //System.out.println("Number of random occurences:"+randomOccurences);

            //step 4 - loop through the number of random occurences
            for (int i = 1; i < randomOccurences; i++) {

                int randomYear = StaticDiceRoller.rolldSomething(randomOccurences, 1) + startYear;
                //System.out.println(i + " -  a random event with a year of " + randomYear);

                //get the length of the events list in the xml, and randomly select one
                String eventVal = TimelineEventGenerator.generateEvent();
                if (!eventVal.equals("") && eventVal.length() > 5) {
                    Object[] myArry = {randomYear, eventVal};
                    SavedTableModel.addRow(myArry);
                    tblTimelineTable.setModel(SavedTableModel);

                }
                tblTimelineTable.setAutoCreateColumnsFromModel(false);
                int mColIndex = 0;
                SortJTableUtil sorter = new SortJTableUtil();
                sorter.sortColumn(SavedTableModel, mColIndex, true);

            }

        } else {
            JOptionPane.showMessageDialog(btnTimelineGenerate, "A start and end year must be specified");
        }
    }
}
